package io.kiki.sba.registry.api.exchange;

import io.kiki.sba.registry.api.exchange.message.Request;


public class RequestException extends RuntimeException {

    private Request request;


    public RequestException(String message, Request request) {
        super(message);
        this.request = request;
    }


    public RequestException(String message, Request request, Throwable cause) {
        super(message, cause);
        this.request = request;
    }

    public RequestException(String message) {
        super(message);
    }


    public RequestException(String message, Throwable cause) {
        super(message, cause);
    }


    public RequestException(Throwable cause) {
        super(cause);
    }


    @Override
    public String getMessage() {
        StringBuilder sb = new StringBuilder(256);
        if (request != null) {
            final Object requestBody = request.getRequestBody();
            String requestBodyStr = null;
            if (requestBody instanceof String) {
                requestBodyStr = requestBody.toString();
            } else
                if (requestBody != null) {
                    requestBodyStr = request.getRequestBody().getClass().getSimpleName();
                }
            sb.append("request url: ").append(request.getRequestUrl()).append(", body: ").append(requestBodyStr).append(", ");
        }
        sb.append(super.getMessage());
        return sb.toString();
    }
}
